package de.htwg_konstanz.ebus.framework.junitTests;

import java.io.File;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import org.junit.Test;

import de.htwg_konstanz.ebus.framework.impEx.IImporter;
import de.htwg_konstanz.ebus.framework.impEx.XmlImporter;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.BMECAT;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.ProductCatalogService;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.ProductCatalogServiceFault;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.ProductCatalogServicePortType;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.ProductCatalogServiceRequest;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.ProductCatalogServiceResponse;


public class WebServiceProxyTest {
	
	@Test
	public void test() {
		ProductCatalogService pcs = new ProductCatalogService();
		ProductCatalogServicePortType portType = pcs.getProductCatalogServicePort();
		
		ProductCatalogServiceRequest request = new ProductCatalogServiceRequest();
		request.setUserName("T12_L1_E1");
		request.setPassword("T12_L1");
		
		try {
			ProductCatalogServiceResponse pcsr = portType.getProductCatalog(request);
			
			BMECAT bmecat = pcsr.getBMECAT();
			
            try {
		        StringWriter sw = new StringWriter();
		        JAXBContext jc = JAXBContext.newInstance("de.htwg_konstanz.ebus.framework.webservices.productcatalog");
		        Marshaller marshaller = jc.createMarshaller();
		        marshaller.marshal(bmecat, new File("resources/upload/importedCatalog.xml"));
		        
		        IImporter xmlImporter = new XmlImporter("resources/import/bmecat_new_catalog_1_2_simple_without_NS.xsd", 
		        										"resources/upload/importedCatalog.xml");
		        
		        System.out.println("--> " + xmlImporter.isValid());
		        
		       //if (xmlImporter.isValid()) {
		        	xmlImporter.performImport();
		       //}
		        
            } catch(Exception e) {
            	e.printStackTrace();
            }
	    
		} catch (ProductCatalogServiceFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e.getFaultInfo());
		}
		
	}
}
